TAR = test
LIBS = ../lib/mylib.a
 
# The directories in which source files reside. 
# At least one path should be specified. 
# SRCDIRS := . # current directory 
SRCDIRS := .

# The source file types (headers excluded). 
# At least one type should be specified. 
# The valid suffixes are among of .c, .C, .cc, .cpp, .CPP, .c++, .cp, or .cxx. 
# SRCEXTS := .c # C program 
# SRCEXTS := .cpp # C++ program 
# SRCEXTS := .c .cpp # C/C++ program 
SRCEXTS := .c

# The flags used by the cpp (man cpp for more). 
# CPPFLAGS := -Wall -Werror # show all warnings and take them as errors 
CPPFLAGS := -g -Wall -Werror -O2

# The compiling flags used only for C. 
# If it is a C++ program, no need to set these flags. 
# If it is a C and C++ merging program, set these flags for the C parts. 
CFLAGS := -g -Wall -Werror -O2


# The compiling flags used only for C++. 
# If it is a C program, no need to set these flags. 
# If it is a C and C++ merging program, set these flags for the C++ parts. 
#CXXFLAGS := -g
 

# The library and the link options ( C and C++ common). 
LDFLAGS := -lsqlite3#-lm -lcurses
LDFLAGS += 

## Implict Section: change the following only when necessary. 
##============================================================================= 
# The C program compiler. Uncomment it to specify yours explicitly. 
CC = gcc 

# The C++ program compiler. Uncomment it to specify yours explicitly. 
CXX = g++

# Uncomment the 2 lines to compile C programs as C++ ones. 
#CC = $(CXX) 
#CFLAGS = $(CXXFLAGS) 

# The command used to delete file. 
RM = rm -f 
SOURCES = $(wildcard $(addprefix *,$(SRCEXTS)))
#SOURCE = $(foreach d,$(SRCDIRS),$(wildcard $(addprefix *,$(SRCEXTS))))
#OBJS = $(SOURCES:%.c=%.o)
OBJS = $(foreach x,$(SRCEXTS),$(patsubst %$(x),%.o,$(filter %$(x),$(SOURCES)))) 

#=============================
.PHONY : all clean rebuild debug

all :version $(TAR) 
# Rules for producing the objects. 
#--------------------------------------------------- 
objs : $(OBJS) 

%.o : %.c 
	$(CC) -c $(CFLAGS) $< 

%.o : %.C 
	$(CXX) -c $(CPPFLAGS) $< 

%.o : %.cc 
	$(CXX) -c $(CPPFLAGS) $< 

%.o : %.cpp 
	$(CXX) -c $(CPPFLAGS) $< 

%.o : %.CPP 
	$(CXX) -c $(CPPFLAGS) $< 

%.o : %.c++ 
	$(CXX -c $(CPPFLAGS) $< 

%.o : %.cp 
	$(CXX) -c $(CPPFLAGS) $< 

%.o : %.cxx 
	$(CXX) -c $(CPPFLAGS) $<
	
#--------------------------------------
version:
	@$(CC) --version
#---------------------------------------------
sublib:
	@$(MAKE) -f lib.mk --no-print-directory -C ../lib all 
subclean:
	@$(MAKE) -f lib.mk --no-print-directory -C ../lib clean
subcleanall:
	@$(MAKE) -f lib.mk --no-print-directory -C ../lib cleanall
# Rules for producing the executable. 
#---------------------------------------------- 
$(TAR) : $(OBJS) 
ifeq ($(strip $(SRCEXTS)), .c) # C file 
	$(CC) -o $(TAR) $^ $(LDFLAGS) 
else # C++ file 
	$(CXX) -o $(TAR) $^ $(LDFLAGS) 
endif 

rebuild: clean all 

clean: 
	$(RM) *.o *.d *.exe
	$(RM) $(TAR) 

debug:
	@echo $(SOURCES)
	@echo $(OBJS)

lintall:
	@echo "lint the sources"
	@$(LINT) $(SOURCES) > $(PROGRAM).lint
	@$(MAKE) -f lib.mk --no-print-directory -C ../lib  lintall
### End of the Makefile ## Suggestions are welcome ## All rights reserved ### 

